package com.jsp.mapper.impl;


import com.jsp.entity.User;
import com.jsp.mapper.UserMapper;
import com.jsp.util.JdbcUtils;

import java.util.List;
import java.util.UUID;

public class UserMapperImpl implements UserMapper {
    @Override
//查询user表的所有值
    public List<User> findAll() {
        String sql = "select YHDM,DWDM,YHID,YHXM,YHKL,YHXB,YHBM,CSRQ,DJSJ,SFJY,PXH from T_USER order by PXH ";
        return JdbcUtils.findAll(User.class, sql, null);
    }
//按照用户id查询
    @Override
    public User findByUsername(String yhid) {
        String sql = "select YHDM,DWDM,YHID,YHXM,YHKL,YHXB,YHBM,CSRQ,DJSJ,SFJY,PXH from T_USER where YHID = ?";
        return JdbcUtils.findByPro(User.class, sql, yhid);
    }
//按照用户代码查询
    @Override
    public User findById(String yhdm) {
        String sql = "select YHDM,DWDM,YHID,YHXM,YHKL,YHXB,YHBM,CSRQ,DJSJ,SFJY,PXH from T_USER where YHDM = ?";
        return JdbcUtils.findByPro(User.class, sql, yhdm);
    }
//添加
    @Override
    public int addUser(User user) {
        String sql = "insert into T_USER values(?,?,?,?,?,?,?,?,?,0,?)";
        return JdbcUtils.update(sql,
                UUID.randomUUID().toString().substring(0, 8),
                "***",
                user.getYhid(),
                user.getYhxm(),
                user.getYhkl(),
                user.getYhxb(),
                user.getYhbm(),
                user.getCsrq(),
                user.getCsrq(),
                user.getPxh()
        );
    }
//按照用户id删除
    @Override
    public int delUser(String yhid) {
        String sql = "delete from T_USER where YHID=?";
        return JdbcUtils.update(sql, yhid);
    }
//按照用户代码更新
    @Override
    public int updateUser(User user) {
        String sql = "update T_USER set DWDM=?,YHID=?,YHXM=?,YHKL=?,YHXB=?,YHBM=?,DJSJ=?,SFJY=?,CSRQ=? where YHDM=?";
        return JdbcUtils.update(sql,
                user.getDwdm(),
                user.getYhid(),
                user.getYhxm(),
                user.getYhkl(),
                user.getYhxb(),
                user.getYhbm(),
                user.getDjsj(),
                user.getSfjy(),
                user.getCsrq(),
                user.getYhdm());
    }
}
